package com.mysiteforme.admin.controller.rfid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.mysiteforme.admin.entity.RfidPotting;
import com.mysiteforme.admin.service.rfid.RfidPottingService;
import com.mysiteforme.admin.util.other.LayerData;
import com.mysiteforme.admin.annotation.SysLog;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.javassist.expr.NewArray;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * RfidPotting 前端控制器
 * </p>
 *
 * @author rober
 * @since 2020-07-28
 */
@Controller
public class WebScrachRfidPottingController {
    private static final Logger LOGGER = LoggerFactory.getLogger(WebScrachRfidPottingController.class);
    @Autowired
    private RfidPottingService rfidPottingService;
    
    @Value("${excelPath}")
    private String excelPath;
    
   
    @GetMapping("scrach/potting")
    @SysLog("跳转RFID查询Potting表列表")
    public ModelAndView  list_Potting(ModelAndView model){
		model.addObject("range", rfidPottingService.lookDateRange());
    	model.setViewName("/admin/rfidPotting/scrach");
		return model;
    }
    
    @PostMapping("scrach/potting")
    @ResponseBody
    @SysLog("RFID查询Potting")
	public LayerData<RfidPotting> list_potting(String rfidRfidNum) {
		LayerData<RfidPotting> layerData = new LayerData<>();
		if (!StringUtils.isBlank(rfidRfidNum)) {
			rfidRfidNum=rfidRfidNum.replaceAll("[\\t\\n\\r]", ",");	
			String[] arr = rfidRfidNum.split(",");
			List<String> RFID =  Arrays.asList(arr);
			layerData.setData(rfidPottingService.scrachRfid(RFID));
		}
		return layerData;
	}
    

    @PostMapping("scrach/pottingData")
    @ResponseBody
    @SysLog("RFID查询Potting")
	public String  list_Data(String rfidRfidNum) {
        List<String> excelName= new ArrayList<String>();
		if (!StringUtils.isBlank(rfidRfidNum)) {
			rfidRfidNum=rfidRfidNum.replaceAll("[\\t\\n\\r]", ",");	
			String[] arr = rfidRfidNum.split(",");
			List<String> RFID =  Arrays.asList(arr);
			excelName = ouputExcelData(rfidPottingService.scrachRfid(RFID));
		}else {
			return null ;
		}
		return excelName.get(0) ;
	}
    
    private List<String> ouputExcelData(List<RfidPotting> list) {
		FileOutputStream fileOut = null;   
        List<String> excelName= new ArrayList<String>();
		try {            
			ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();                                  
			InputStream in = new FileInputStream(excelPath+"/rfid.xls");
			HSSFWorkbook wb = new HSSFWorkbook(in);    
			HSSFSheet sheet1 = wb.getSheetAt(0);
            HSSFCellStyle setBorder = wb.createCellStyle();
		    HSSFFont font = wb.createFont();
	         font.setFontName("微软雅黑");
	         font.setFontHeightInPoints((short) 9);//设置字体大小
	         setBorder.setFont(font);
			 System.out.println("我是"+list);
			 for (int i = 0 ; i < list.size(); i++) {
				 HSSFRow row = sheet1.getRow(i+1);
			       Cell cell0 = row.createCell(0);       //创建单元格
			       Cell cell1 = row.createCell(1);      //创建单元格
			       Cell cell2 = row.createCell(2);      //创建单元格
			       Cell cell3 = row.createCell(3);      //创建单元格
			       Cell cell4 = row.createCell(4);      //创建单元格
			       Cell cell5 = row.createCell(5);      //创建单元格
			       Cell cell6 = row.createCell(6);      //创建单元格
                   cell0.setCellValue(list.get(i).getRfidRfidNum());        //赋值
                   cell1.setCellValue(list.get(i).getRfidBatchNum());        //赋值
                   cell2.setCellValue(list.get(i).getRfidSheet());        //赋值
                   cell3.setCellValue(list.get(i).getRfidSheetId());        //赋值
                   cell4.setCellValue(list.get(i).getRfidState());        //赋值
                   cell5.setCellValue(list.get(i).getRemarks());        //赋值
                   CreationHelper createHelper=wb.getCreationHelper();
    		       setBorder.setDataFormat(createHelper.createDataFormat().getFormat("yyy-mm-dd hh:mm:ss"));
			       cell6.setCellStyle(setBorder);  
                   cell6.setCellValue(list.get(i).getCreateDate());        //赋值
			}     

		   SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd-HH-mm");
            excelName.add(0, dateformat.format(new Date())+UUID.randomUUID().toString()+".xls");
            excelName.add(1, excelPath+ excelName.get(0));
			fileOut = new FileOutputStream(excelName.get(1));               // 写入excel文件                
			wb.write(fileOut);  
			    if ( in != null) {
			    	 in.close();
	             }
			} catch (Exception e) {            
				e.printStackTrace();     
				}finally{            
					if(fileOut != null){                 
						try {                    
							fileOut.close();               
							} catch (IOException e) {                   
								e.printStackTrace();               
								}      
						} 			 
					}   
            return  excelName;
		} 
}